package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.dao.CategoryDao;
import com.itheima.dao.DishDao;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private DishDao dishDao;
    @Autowired
    private SetmealDao setmealDao;
    @Override
    public R checkDelete(Long id) {
        LambdaQueryWrapper<Dish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Dish::getCategoryId,id);
        Integer dishCount = dishDao.selectCount(lqw1);
        if (dishCount > 0){
            return R.error("当前分类下有菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Setmeal::getCategoryId,id);
        Integer setmealCount = setmealDao.selectCount(lqw2);
        if (setmealCount > 0 ){
            return R.error("当前分类下有套餐，不能删除");
        }
        int byId = categoryDao.deleteById(id);
        return R.success(byId);
    }

    @Override
    public List<Category> getCategoryType(Integer type) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type != null && type != 0,Category::getType,type);
        lqw.orderByDesc(Category::getSort);
        List<Category> categoryList = categoryDao.selectList(lqw);
        return categoryList;
    }
}
